package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-13
 */
@Api()
//@ApiModel(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    private EduTeacherService service;
    @Autowired
    public void setService(EduTeacherService service) {
        this.service = service;
    }

    @ApiOperation(value = "返回所有讲师信息.")
    @GetMapping(path = "/findAll")
    public R findAllTeacher(){
        List<EduTeacher> teachers = service.list(null);
        return R.ok().data("items",teachers);
    }

    @ApiOperation(value = "按照传入ID删除讲师信息.")
    @DeleteMapping(path = "/delete/{id}")
    public R remoteTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable(name = "id") String id){
        boolean flush = service.removeById(id);
        if(flush){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "教师分页查询.")
    @GetMapping(path = "pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        //填入当前页数与当前页所含项目数
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用教师service把数据封装进pageTeacher中
        service.page(pageTeacher,null);
        //取出数据
        long total = pageTeacher.getTotal();
        List<EduTeacher> teachers = pageTeacher.getRecords();
        //封装进返回json信息
        return R.ok().data("total",total).data("rows",teachers);
    }

    @ApiOperation(value = "教师按照传入条件来进行分页查询.")
    @PostMapping(path = "pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //取出查询封装的数据
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //拼接动态SQL语句
        if(!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("begin",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("end",end);
        }
        //根据创建时间排序
        wrapper.orderByDesc("gmt_create");
        //将条件wrapper封装进service进行查询,然后将放回的结果封装进page
        service.page(pageTeacher,wrapper);
        //取出数据
        long total = pageTeacher.getTotal();
        List<EduTeacher> teachers = pageTeacher.getRecords();
        //封装进返回json信息
        return R.ok().data("total",total).data("rows",teachers);
    }

    @ApiOperation(value = "保存教师信息.")
    @PostMapping(path = "/addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean save = service.save(teacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "根据教师ID来获取教师信息.")
    @GetMapping(path = "/getTeacher/{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher teacher = service.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation(value = "更新此ID的教师信息.")
    @PostMapping(path = "/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean flush = service.updateById(teacher);
        if(flush){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

